{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-24T03:01:46.190175Z",
     "start_time": "2025-11-24T03:01:46.174226Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "\"\"\"\n",
    "某产品过去12个月的销售量Series:\n",
    "1.计算季度平均销量(每3个月为一个季度)\n",
    "2.找出销量最高的月份\n",
    "3.计算月环比增长率\n",
    "4.找出连续增长超过2个月的月份\n",
    "\"\"\""
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n某产品过去12个月的销售量Series:\\n1.计算季度平均销量(每3个月为一个季度)\\n2.找出销量最高的月份\\n3.计算月环比增长率\\n4.找出连续增长超过2个月的月份\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T01:44:41.654454Z",
     "start_time": "2025-11-24T01:44:41.615704Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据准备\n",
    "sales =pd.Series([120,135,145,160,155,170,180,175,190,200,210,220],\n",
    "                 index=pd.date_range('2022-01-01', periods=12, freq='MS'))\n",
    "print(sales)"
   ],
   "id": "963d456a373d7f06",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-01-01    120\n",
      "2022-02-01    135\n",
      "2022-03-01    145\n",
      "2022-04-01    160\n",
      "2022-05-01    155\n",
      "2022-06-01    170\n",
      "2022-07-01    180\n",
      "2022-08-01    175\n",
      "2022-09-01    190\n",
      "2022-10-01    200\n",
      "2022-11-01    210\n",
      "2022-12-01    220\n",
      "Freq: MS, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T02:48:07.986513Z",
     "start_time": "2025-11-24T02:48:07.977847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.计算季度平均销量(每3个月为一个季度)\n",
    "sales.resample('QS').mean()"
   ],
   "id": "c59c98895268e6cc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01    133.333333\n",
       "2022-04-01    161.666667\n",
       "2022-07-01    181.666667\n",
       "2022-10-01    210.000000\n",
       "Freq: QS-JAN, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T02:49:01.878254Z",
     "start_time": "2025-11-24T02:49:01.873094Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2.找出销量最高的月份\n",
    "print(sales.idxmax())"
   ],
   "id": "7010ba3cefe0bc91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-12-01 00:00:00\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T02:57:47.225270Z",
     "start_time": "2025-11-24T02:57:47.172673Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3.计算月环比增长率（和“每日收益率”一样的算法，本月-前月）\n",
    "sales.pct_change()"
   ],
   "id": "6929a7ddc207e4ff",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01         NaN\n",
       "2022-02-01    0.125000\n",
       "2022-03-01    0.074074\n",
       "2022-04-01    0.103448\n",
       "2022-05-01   -0.031250\n",
       "2022-06-01    0.096774\n",
       "2022-07-01    0.058824\n",
       "2022-08-01   -0.027778\n",
       "2022-09-01    0.085714\n",
       "2022-10-01    0.052632\n",
       "2022-11-01    0.050000\n",
       "2022-12-01    0.047619\n",
       "Freq: MS, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T03:10:26.744476Z",
     "start_time": "2025-11-24T03:10:26.730227Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4.找出连续增长超过2个月的月份\n",
    "a = sales.pct_change()\n",
    "b = a > 0\n",
    "print(\"方法一：用眼睛找\")\n",
    "print(b)\n",
    "print(\"方法二：滑动窗口\")\n",
    "b[b.rolling(3).sum()==3].keys().tolist()"
   ],
   "id": "6be85f41c667de1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法一：用眼睛找\n",
      "2022-01-01    False\n",
      "2022-02-01     True\n",
      "2022-03-01     True\n",
      "2022-04-01     True\n",
      "2022-05-01    False\n",
      "2022-06-01     True\n",
      "2022-07-01     True\n",
      "2022-08-01    False\n",
      "2022-09-01     True\n",
      "2022-10-01     True\n",
      "2022-11-01     True\n",
      "2022-12-01     True\n",
      "Freq: MS, dtype: bool\n",
      "方法二：滑动窗口\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Timestamp('2022-04-01 00:00:00'),\n",
       " Timestamp('2022-11-01 00:00:00'),\n",
       " Timestamp('2022-12-01 00:00:00')]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
